package top.shen33.yang.services.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.shen33.yang.model.entity.RbacRole;
import top.shen33.yang.model.entity.RbacUser;
import top.shen33.yang.mapper.RbacUserMapper;
import top.shen33.yang.model.entity.RbacUserRole;
import top.shen33.yang.services.RbacRoleService;
import top.shen33.yang.services.RbacUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author oreilly
 * @since 2018-08-28
 */
@Service
public class RbacUserServiceImpl extends ServiceImpl<RbacUserMapper, RbacUser>implements RbacUserService {


    @Autowired
    private RbacRoleService roleService;


    @Override
    public Class clazz() {
        return RbacUser.class;
    }

    @Override
    @Transactional
    public void doAdd(RbacUser itemRbacUser) {

        itemRbacUser.insertOrUpdate();

        doUpdateRel(itemRbacUser);
    }

    @Override
    @Transactional
    public void doUpdate(RbacUser itemRbacUser) {

        itemRbacUser.insertOrUpdate();

        doUpdateRel(itemRbacUser);
    }

    private void doUpdateRel(RbacUser itemRbacUser) {
        Integer userSid = itemRbacUser.getSid();
        List<Integer> roleIds = itemRbacUser.getRoleIds();
        if(roleIds != null && roleIds.size()>0){

            QueryWrapper<RbacUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userSid);
            RbacUserRole userRole =new RbacUserRole();
            userRole.delete(wrapper);

            for (Integer roleId : roleIds) {
                RbacUserRole userRoleItem =new RbacUserRole();
                userRoleItem.setUserId(userSid);
                userRoleItem.setRoleId(roleId);
                userRoleItem.insertOrUpdate();
            }

        }
    }

    @Override
    @Transactional
    public void doDelete(String[] ids) {

        QueryWrapper<RbacUser> ew = new QueryWrapper<>();
        ew.in("sid", ids);
        new RbacUser().delete(ew);
    }


    @Override
    public List<RbacUser> all(RbacUser itemRbacUser) {

        return this.baseMapper.all(itemRbacUser);
    }


        @Override
    public Page<RbacUser> pageInfo(Page page, RbacUser itemRbacUser) {

        List<RbacUser> dataList = this.baseMapper.pageInfo(page, itemRbacUser);
        if(page ==null ){
            page = new Page(0,10000);
        }
        page.setRecords(dataList);
        return page;
    }


    @Override
    @Transactional
    public void doImport(List<RbacUser> dataList) {

        //数据验证

        //保存
        this.saveOrUpdateBatch(dataList);
    }


    @Override
    public RbacUser getOne(String sid) {

        RbacUser rbacUser = getById(sid);

        List<RbacRole> roleList = roleService.getRoleListByUserId(sid);

        if(roleList !=null  && roleList.size()>0){
            List<Integer> roleIds = roleList.stream().map(role -> role.getSid()).collect(Collectors.toList());
            rbacUser.setRoleIds(roleIds);
        }
        rbacUser.setRbacRoleList(roleList);
        return rbacUser;
    }
}
